﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;

namespace PDFTool.Common
{
    class PdfReader
    {

        public static void ReadPdf2Image(string pdfFile, string outputPath)
        {

            int desired_dpi = 200;

            using (var rasterizer = new GhostscriptRasterizer())
            {
                using (var fs = File.OpenRead(pdfFile))
                {
                    rasterizer.Open(fs);
                    string name = Path.GetFileNameWithoutExtension(pdfFile);
                    for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
                    {
                        var pageFilePath = Path.Combine(outputPath, string.Format("{0}-{1}.png", name, pageNumber));

                        var img = rasterizer.GetPage(desired_dpi, pageNumber);
                        if (img == null)
                        {
                            Console.WriteLine("image is null");
                            continue;
                        }
                        img.Save(pageFilePath, ImageFormat.Png);

                        Console.WriteLine(pageFilePath);
                    }
                }
            }

        }
    }
}
